<?php
// 新建投诉
class QuestiondelAction extends CommonAction {
    function _filter(&$map)
    {
        $map['number'] = array('like', "%" . $_POST['number'] . "%");
        $map['address'] = array('like', "%" . $_POST['address'] . "%");
        if($_SESSION[city]>100){
            $map['postcity'] = $_SESSION[city];
        }
        $map['isdel']=1;
        if(!isset($_SESSION['Qusetiondel_status'])){
            $map['status']=3;
        }elseif(!isset($_POST['status'])){
            $map['status']=$_SESSION['Qusetiondel_status'];
        }else{
            $map['status']=$_POST['status'];
        }
        $map['isdel']=1;
    }
    public function index() {
        $name="Question";
        $map = $this->_search ($name);
        if (method_exists ( $this, '_filter' )) {
            $this->_filter ( $map );
        }

        $model = D ($name);
        if (! empty ( $model )) {
            $this->_list ( $model, $map );
        }
        $map_status_list=array(0=>'待办',1=>'在办',2=>'办结',3=>'全部');
        $this->assign ( 'map_status_list', $map_status_list );
        $_SESSION["Qusetiondel_status"]=$map['status'];
        $this->display ();
        return;
    }
    protected function _list($model, $map, $sortBy = '', $asc = false, $countPk = 'id') {
        //排序字段 默认为主键名
        if (!empty ( $_REQUEST ['_order'] )) {
            $order = $_REQUEST ['_order'];
        } else {
            $order = ! empty ( $sortBy ) ? $sortBy : $model->getPk ();
        }
        //排序方式默认按照倒序排列
        //接受 sost参数 0 表示倒序 非0都 表示正序
        if (isset ( $_REQUEST ['_sort'] )) {
//			$sort = $_REQUEST ['_sort'] ? 'asc' : 'desc';
            $sort = $_REQUEST ['_sort'] == 'asc' ? 'asc' : 'desc'; //zhanghuihua@msn.com
        } else {
            $sort = $asc ? 'asc' : 'desc';
        }
        //取得满足条件的记录数
        if($map['status']==3){
            $map['status']=array('ELT', 2);
        }
        $count = $model->where ( $map )->count ( $countPk );
        if ($count > 0) {
            import ( "@.ORG.Util.Page" );
            //创建分页对象
            if (! empty ( $_REQUEST ['listRows'] )) {
                $listRows = $_REQUEST ['listRows'];
            } else {
                $listRows = '';
            }
            $p = new Page ( $count, $listRows );
            //分页查询数据
            $voList = $model->where($map)->order( "`" . $order . "` " . $sort)->limit($p->firstRow . ',' . $p->listRows)->select ( );

            //echo $model->getlastsql();
            //分页跳转的时候保证查询条件
            foreach ( $map as $key => $val ) {
                if (! is_array ( $val )) {
                    $p->parameter .= "$key=" . urlencode ( $val ) . "&";
                }
            }
            //分页显示
            $page = $p->show ();
            //列表排序显示
            $sortImg = $sort; //排序图标
            $sortAlt = $sort == 'desc' ? '升序排列' : '倒序排列'; //排序提示
            $sort = $sort == 'desc' ? 1 : 0; //排序方式

            $reason_list=C(QUESTIONREASON);
            $this->assign ( 'reason_list', $reason_list );
            $status_list=C(STATUS);
            $this->assign ( 'status_list', $status_list );
            $city_list = C(CITY);
            $this->assign('city_list', $city_list);
            $jiaose_list = C(JIAOSE);
            $this->assign('jiaose_list', $jiaose_list);
            //模板赋值显示
            $this->assign ( 'list', $voList );
            $this->assign ( 'sort', $sort );
            $this->assign ( 'order', $order );
            $this->assign ( 'sortImg', $sortImg );
            $this->assign ( 'sortType', $sortAlt );
            $this->assign ( "page", $page );
        }
        $this->assign ( 'totalCount', $count );
        $this->assign ( 'numPerPage', $p->listRows );
        $this->assign ( 'currentPage', !empty($_REQUEST[C('VAR_PAGE')])?$_REQUEST[C('VAR_PAGE')]:1);

        Cookie::set ( '_currentUrl_', __SELF__ );
        return;
    }


    public function _before_add(){
        $Group = D('Role');
        //查找满足条件的列表数据
        $reason_list=C(QUESTIONREASON);
        $this->assign('reason_list',$reason_list);

    }

    function read() {
        $name="Question";
        $model = M ( $name );
        $vo = $model->getById ( $_GET[pid] );
        $reason_list=C(QUESTIONREASON);
        $this->assign('reason_list',$reason_list);
        $status_list=C(STATUS);
        $city_list = C(CITY);
        $po= M("Process")->where('questionid='.$_GET[pid])->order('reply_time')->select();
        $numbercount=$model->where('number = '.$_GET[pnumber] .' and isdel=1')->count();
        $this->assign('numbercount', $numbercount);
        $this->assign ( 'polist', $po );
        $this->assign('city_list', $city_list);
        $this->assign('status_list',$status_list);
        $this->assign ( 'vo', $vo );
        $this->display ();
    }


    public function foreverdelete() {
        //删除指定记录
        $name="Question";
        $model = D ($name);
        if (! empty ( $model )) {
            $pk = $model->getPk ();
            $id = $_REQUEST [$pk];
            if (isset ( $id )) {
                $condition = array ($pk => array ('in', explode ( ',', $id ) ) );
                $data["isdel"]=0;
                if (false !== $model->where ( $condition )->save($data)) {
                    //echo $model->getlastsql();
                    $this->success ('删除成功！');
                } else {
                    $this->error ('删除失败！');
                }
            } else {
                $this->error ( '非法操作' );
            }
        }
        $this->forward ();
    }

}
?>